<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "song".
 *
 * @property integer $id
 * @property string $name
 * @property string $alias
 * @property integer $position
 * @property integer $duration
 * @property string $mp3_url
 * @property integer $album_id
 * @property integer $created_at
 * @property integer $updated_at
 */
class Song extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'song';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'alias', 'position', 'duration', 'mp3_url', 'album_id'], 'required'],
            [['position', 'duration', 'album_id'], 'integer'],
            [['name', 'alias', 'mp3_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '歌曲id'),
            'name' => Yii::t('app', '名称'),
            'alias' => Yii::t('app', '别名'),
            'position' => Yii::t('app', '偏移量'),
            'duration' => Yii::t('app', '总量'),
            'mp3_url' => Yii::t('app', '路径'),
            'album_id' => Yii::t('app', '专辑id'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '修改时间'),
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }
    /**
     * @inheritdoc
     * @return SongQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new SongQuery(get_called_class());
    }

    public function getAlbum(){
        return $this->hasOne(Album::className(),['id'=>'album_id']);
    }
}
